<?php
/**
*	@package UHU_Components
*	@subpackage Ping
**/

/**
*	@package UHU_exception
*	 @subpackage Ping
**/
class ping_package_exception extends uhu_exception
{
	protected function getExtendedMessage($code=null)
	{
		$message = '%s';
		switch($code)
		{
			case 100:
			case 101:
				$message = 'Could not execute ping: %s';
			break;
			
			case 200:
				$message = 'Could not add ping sub-service: %s';
			break;
		}
		return $message;
	}
}

/**
*	@package Ping
*	@subpackage Ping_package_pinger
**/
abstract class ping_package_pinger implements SplObserver
{
	abstract protected function url(ping_package $ping_package);
}

/**
*	@package Ping
*	@subpackage Ping_package
**/
abstract class ping_package implements SplSubject, SplObserver
{
	protected $resource;
	protected $observers = array();
	abstract public function resource();
	public function attach(SplObserver $observer)
	{
		if(in_array($observer,$this->observers))
		{
			throw new ping_package_exception(get_class($observer) . ' already attached',200);
		}
		else
		{
			$this->observers[] = $observer;
		}
	}
	public function attachMulti()
	{
		$args = func_get_args();
		foreach($args as $arg)
		{
			$this->attach($arg);
		}
	}
	public function detach(SplObserver $observer)
	{
		if(($pos = array_search($observer,$this->observers)) !== false)
		{
			unset($this->observers[$pos]);
		}
	}
	public function notify()
	{
		foreach($this->observers as $observer)
		{
			$observer->update($this);
		}
	}
	public function update(SplSubject $subject)
	{
		$this->notify();
	}
}
?>